#! /bin/bash

clear
echo "+------------------------------------------------------------------------+"
echo "|                   DNRMMP V1.0 By douxuefeng                            |"
echo "+------------------------------------------------------------------------+"
echo "|           A tool to auto-compile & install DNRMMP on Linux             |"
echo "+------------------------------------------------------------------------+"
echo "|     For more information please visit https://gitee.com/douxuefeng     |"
echo "+------------------------------------------------------------------------+"

Path=$(pwd)

DockerFilePath=$Path/dockerfile/ihy-php/

Network="ubuntu"

apt update && apt upgrade -y

if [ -z $(docker info) ]; then

	echo "正在安装docker环境..."

	apt-get remove docker docker-engine docker.io

	apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common

	curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

	add-apt-repository \
	   "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
	   $(lsb_release -cs) \
	   stable"

	apt update && apt install docker-ce -y
fi


echo "正在检查网络情况,并创建网络..." 

if [ -z $(docker network ls | grep $Network | awk '{print $2}') ]; then

	docker network create --driver bridge --subnet=172.18.12.0/16 --gateway=172.18.12.1 $Network
fi

echo "网络安装成功"

#php7.3

echo "正在检查PHP环境,并创建PHP环境..." 

PHP=$(docker images | grep php7.3 | awk '{print $3}')

if [ -z  $PHP ] ; then

	docker build -t php7.3 ${DockerFilePath}

	PHP=$(docker images | grep php7.3 | awk '{print $3}')
fi

docker run --name php --network $Network --network-alias php -v $Path/www:/var/www/html -v $Path/php/php.ini:/usr/local/etc/php/php.ini --privileged=true --restart=always --ip=172.18.12.3 -d $PHP 

echo "PHP环境创建成功"

# mariadb:10.4-bionic

echo "正在安装mariadb:10.4-bionic..." 

if [ -z $(docker ps | grep mariadb:10.4-bionic ) ]; then

	docker run --name mariadb --network $Network --network-alias mariadb -e MYSQL_ROOT_PASSWORD=root -v $Path/mariadb/data:/var/lib/mysql -v $Path/mariadb/conf:/etc/mysql --privileged=true --restart=always --ip=172.18.12.4 -d mariadb:10.4-bionic 
fi

echo "安装mariadb:10.4-bionic成功" 


#nginx:1.16

echo "正在安装nginx:1.16..."

if [ -z $(docker ps | grep nginx:1.16) ]; then
	
	docker run -d -p 80:80 -p 443:443 --network $Network --network-alias nginx --name nginx -v $Path/nginx:/etc/nginx -v $Path/www:/usr/share/nginx --privileged=true --restart=always --ip=172.18.12.5 nginx:1.16 
fi

echo "安装nginx:1.16成功"

#redis

echo "正在安装redis:latest..."

if [ -z $(docker ps | grep redis) ]; then

	docker run -d --network $Network --network-alias redis --name redis --restart=always --ip=172.18.12.6 redis:latest 
fi

echo "安装redis:latest成功"
 
#rabbitmq

echo "正在安装rabbitmq:3.7.17..."

if [ -z $(docker ps | grep rabbitmq) ]; then
	
	docker run -d --name rabbitmq --network $Network --network-alias rabbitmq --privileged=true --restart=always --ip=172.18.12.7 -v $Path/rabbitmq/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf -v $Path/rabbitmq/database:/var/lib/rabbitmq rabbitmq:3.7.17
fi

# 安装swoole 使用websoket聊天
echo "正在安装swoole..."

if [ -z $(docker ps | grep swoole) ]; then
	
	docker run -d --name swoole --network $Network --network-alias swoole --privileged=true --restart=always --ip=172.18.12.8 -v $Path/swoole:/app twosee/swoole php /app/server.php start
fi

# docker run -d --name swoole --network ubuntu --network-alias swoole --restart=always --ip=172.18.12.8 -v /home/ubuntu/jobs/data/swoole:/app twosee/swoole php /app/server.php start

# 安装mongodb 

echo "正在安装mongodb:4.2.0-bionic..."

if [ -z $(docker ps | grep mongodb) ]; then
	
	docker run -d --name mongo --net $Network --network-alias mongo --privileged=true --restart=always --ip=172.18.12.9 -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=root -v $Path/mongo:/data/db mongo:4.2.0-bionic
fi

echo "安装完毕，退出安装"

rm -rf $DockerFilePath

exit



